************Pre-treatment Analysis: Inmate Level**********
cd "C:\Users\xinto\Dropbox\Vietnam Veteran Paper\Crime Paper\2020 JHR Data\7. Table 6\Inmate Level"

set more off
clear all
set memory 350m
set matsize 800

use inmate_data, clear 
keep if white!=9
gen T=vv
gen Z=eligible
***Restricted to 1948-1952 male cohort: white**********
keep if totalSample==1
count 
sca n_all=r(N)

*never-taker
gen nt=1 if T==0 & Z==1
replace nt=0 if nt==.
*always-taker
gen at=1 if T==1 & Z==0
replace at=0 if at==.
*always-taker and complier 
gen atC=1 if T==1 & Z==1
replace atC=0 if atC==.
*never-taker and complier 
gen ntC=1 if T==0 & Z==0
replace ntC=0 if ntC==.

mean nt if Z==1
mean at if Z==0
mean atC if Z==1
mean ntC if Z==0

/*Variable Sample Observation */ 
*First arrested before 18 year old  
count if firstArrestBy18!=9 
sca num_firstArrestBy18=r(N)
sca li num_firstArrestBy18

*On probation before 18 years old 
count if probationBy18!=9 
sca num_probationBy18=r(N)
sca li num_probationBy18

*Any incarceration before 1968
count if incarcerationBefore1968!=9
sca num_incarcerationBefore1968=r(N)
sca li num_incarcerationBefore1968

** Conducting ttests and prtests for the baseline characteristics  for the strata never-takers and always-takers
gen ntAt=.
replace ntAt=1 if at==1
replace ntAt=0 if nt==1




*First arrested before 18 year old  
tab firstArrestBy18
svyset [pweight=weight]
svy: mean firstArrestBy18 if firstArrestBy18!=9, over(ntAt) 
mat result=r(table)
sca firstArrestBy18_nt=result[1,1]
sca list firstArrestBy18_nt
sca firstArrestBy18_nt_se=result[2,1]
sca list firstArrestBy18_nt_se
sca firstArrestBy18_at=result[1,2]
sca list firstArrestBy18_at
sca firstArrestBy18_at_se=result[2,2]
sca list firstArrestBy18_at_se
lincom [firstArrestBy18]0-[firstArrestBy18]1
sca firstArrestBy18_ntat=r(estimate)
sca list firstArrestBy18_ntat
sca se_firstArrestBy18_ntat=r(se)
sca list se_firstArrestBy18_ntat

*On probation before 18 years old 
tab probationBy18
svyset [pweight=weight]
svy: mean probationBy18 if probationBy18!=9, over(ntAt) 
mat result=r(table)
sca probationBy18_nt=result[1,1]
sca list probationBy18_nt
sca probationBy18_nt_se=result[2,1]
sca list probationBy18_nt_se
sca probationBy18_at=result[1,2]
sca list probationBy18_at
sca probationBy18_at_se=result[2,2]
sca list probationBy18_at_se
lincom [probationBy18]0-[probationBy18]1
sca probationBy18_ntat=r(estimate)
sca list probationBy18_ntat
sca se_probationBy18_ntat=r(se)
sca list se_probationBy18_ntat

*Any incarceration before 1968
tab incarcerationBefore1968
svyset [pweight=weight]
svy: mean incarcerationBefore1968 if incarcerationBefore1968!=9, over(ntAt) 
mat result=r(table)
sca incarcerationBefore1968_nt=result[1,1]
sca list incarcerationBefore1968_nt
sca incarcerationBefore1968_nt_se=result[2,1]
sca list incarcerationBefore1968_nt_se
sca incarcerationBefore1968_at=result[1,2]
sca list incarcerationBefore1968_at
sca incarcerationBefore1968_at_se=result[2,2]
sca list incarcerationBefore1968_at_se
lincom [incarcerationBefore1968]0-[incarcerationBefore1968]1
sca incarcerationBefore1968_ntat=r(estimate)
sca list incarcerationBefore1968_ntat
sca se_incarcerationBefore1968_ntat=r(se)
sca list se_incarcerationBefore1968_ntat

** Conducting ttests and prtests for the baseline characteristics  for the strata never-takers v.s. always-takers & c
**Generate a unified indicator between nt v.s. at&c
gen indNtAtC=.
replace indNtAtC=1 if atC==1
replace indNtAtC=0 if nt==1
tab indNtAtC

*First arrested before 18 year old  
tab firstArrestBy18
svyset [pweight=weight]
svy: mean firstArrestBy18 if firstArrestBy18!=9, over(indNtAtC) 
mat result=r(table)
sca firstArrestBy18_nt=result[1,1]
sca list firstArrestBy18_nt
sca firstArrestBy18_nt_se=result[2,1]
sca list firstArrestBy18_nt_se
sca firstArrestBy18_atC=result[1,2]
sca list firstArrestBy18_atC
sca firstArrestBy18_atC_se=result[2,2]
sca list firstArrestBy18_atC_se
lincom [firstArrestBy18]0-[firstArrestBy18]1
sca firstArrestBy18_ntatC=r(estimate)
sca list firstArrestBy18_ntatC
sca firstArrestBy18_ntatC_se=r(se)
sca list firstArrestBy18_ntatC_se

*On probation before 18 years old 
tab probationBy18
svyset [pweight=weight]
svy: mean probationBy18 if probationBy18!=9, over(indNtAtC) 
mat result=r(table)
sca probationBy18_nt=result[1,1]
sca list probationBy18_nt
sca probationBy18_nt_se=result[2,1]
sca list probationBy18_nt_se
sca probationBy18_atC=result[1,2]
sca list probationBy18_atC
sca probationBy18_atC_se=result[2,2]
sca list probationBy18_atC_se
lincom [probationBy18]0-[probationBy18]1
sca probationBy18_ntatC=r(estimate)
sca list probationBy18_ntatC
sca probationBy18_ntatC_se=r(se)
sca list probationBy18_ntatC_se

*Any incarceration before 1968
tab incarcerationBefore1968
svyset [pweight=weight]
svy: mean incarcerationBefore1968 if incarcerationBefore1968!=9, over(indNtAtC) 
mat result=r(table)
sca incarcerationBefore1968_nt=result[1,1]
sca list incarcerationBefore1968_nt
sca incarcerationBefore1968_nt_se=result[2,1]
sca list incarcerationBefore1968_nt_se
sca incarcerationBefore1968_atC=result[1,2]
sca list incarcerationBefore1968_atC
sca incarcerationBefore1968_atC_se=result[2,2]
sca list incarcerationBefore1968_atC_se
lincom [incarcerationBefore1968]0-[incarcerationBefore1968]1
sca incarcerationBefore1968_ntatC=r(estimate)
sca list incarcerationBefore1968_ntatC
sca incarcerationBefore1968_ntatC_se=r(se)
sca list incarcerationBefore1968_ntatC_se


** Conducting ttests and prtests for the baseline characteristics  for the strata always-takers v.s. always-takers & c
**Generate a unified indicator between at v.s. at&c
gen indAtC=.
replace indAtC=1 if atC==1
replace indAtC=0 if at==1
tab indAtC

*First arrested before 18 year old  
tab firstArrestBy18
svyset [pweight=weight]
svy: mean firstArrestBy18 if firstArrestBy18!=9, over(indAtC) 
mat result=r(table)
sca firstArrestBy18_at=result[1,1]
sca list firstArrestBy18_at
sca firstArrestBy18_at_se=result[2,1]
sca list firstArrestBy18_at_se
sca firstArrestBy18_atC=result[1,2]
sca list firstArrestBy18_atC
sca firstArrestBy18_atC_se=result[2,2]
sca list firstArrestBy18_atC_se
lincom [firstArrestBy18]0-[firstArrestBy18]1
sca firstArrestBy18_atatC=r(estimate)
sca list firstArrestBy18_atatC
sca firstArrestBy18_atatC_se=r(se)
sca list firstArrestBy18_atatC_se

*On probation before 18 years old 
tab probationBy18
svyset [pweight=weight]
svy: mean probationBy18 if probationBy18!=9, over(indAtC) 
mat result=r(table)
sca probationBy18_at=result[1,1]
sca list probationBy18_at
sca probationBy18_at_se=result[2,1]
sca list probationBy18_at_se
sca probationBy18_atC=result[1,2]
sca list probationBy18_atC
sca probationBy18_atC_se=result[2,2]
sca list probationBy18_atC_se
lincom [probationBy18]0-[probationBy18]1
sca probationBy18_atatC=r(estimate)
sca list probationBy18_atatC
sca probationBy18_atatC_se=r(se)
sca list probationBy18_atatC_se

*Any incarceration before 1968
tab incarcerationBefore1968
svyset [pweight=weight]
svy: mean incarcerationBefore1968 if incarcerationBefore1968!=9, over(indAtC) 
mat result=r(table)
sca incarcerationBefore1968_at=result[1,1]
sca list incarcerationBefore1968_at
sca incarcerationBefore1968_at_se=result[2,1]
sca list incarcerationBefore1968_at_se
sca incarcerationBefore1968_atC=result[1,2]
sca list incarcerationBefore1968_atC
sca incarcerationBefore1968_atC_se=result[2,2]
sca list incarcerationBefore1968_atC_se
lincom [incarcerationBefore1968]0-[incarcerationBefore1968]1
sca incarcerationBefore1968_atatC=r(estimate)
sca list incarcerationBefore1968_atatC
sca incarcerationBefore1968_atatC_se=r(se)
sca list incarcerationBefore1968_atatC_se


** Conducting ttests and prtests for the baseline characteristics  for the strata never-takers v.s. never-takers & c
**Generate a unified indicator between at v.s. at&c
gen indNtC=.
replace indNtC=1 if ntC==1
replace indNtC=0 if nt==1
tab indNtC


*First arrested before 18 year old  
tab firstArrestBy18
svyset [pweight=weight]
svy: mean firstArrestBy18 if firstArrestBy18!=9, over(indNtC) 
mat result=r(table)
sca firstArrestBy18_nt=result[1,1]
sca list firstArrestBy18_nt
sca firstArrestBy18_nt_se=result[2,1]
sca list firstArrestBy18_nt_se
sca firstArrestBy18_ntC=result[1,2]
sca list firstArrestBy18_ntC
sca firstArrestBy18_ntC_se=result[2,2]
sca list firstArrestBy18_ntC_se
lincom [firstArrestBy18]0-[firstArrestBy18]1
sca firstArrestBy18_ntntC=r(estimate)
sca list firstArrestBy18_ntntC
sca firstArrestBy18_ntntC_se=r(se)
sca list firstArrestBy18_ntntC_se

*On probation before 18 years old 
tab probationBy18
svyset [pweight=weight]
svy: mean probationBy18 if probationBy18!=9, over(indNtC) 
mat result=r(table)
sca probationBy18_nt=result[1,1]
sca list probationBy18_nt
sca probationBy18_nt_se=result[2,1]
sca list probationBy18_nt_se
sca probationBy18_ntC=result[1,2]
sca list probationBy18_ntC
sca probationBy18_ntC_se=result[2,2]
sca list probationBy18_ntC_se
lincom [probationBy18]0-[probationBy18]1
sca probationBy18_ntntC=r(estimate)
sca list probationBy18_ntntC
sca probationBy18_ntntC_se=r(se)
sca list probationBy18_ntntC_se

*Any incarceration before 1968
tab incarcerationBefore1968
svyset [pweight=weight]
svy: mean incarcerationBefore1968 if incarcerationBefore1968!=9, over(indNtC) 
mat result=r(table)
sca incarcerationBefore1968_nt=result[1,1]
sca list incarcerationBefore1968_nt
sca incarcerationBefore1968_nt_se=result[2,1]
sca list incarcerationBefore1968_nt_se
sca incarcerationBefore1968_ntC=result[1,2]
sca list incarcerationBefore1968_ntC
sca incarcerationBefore1968_ntC_se=result[2,2]
sca list incarcerationBefore1968_ntC_se
lincom [incarcerationBefore1968]0-[incarcerationBefore1968]1
sca incarcerationBefore1968_ntntC=r(estimate)
sca list incarcerationBefore1968_ntntC
sca incarcerationBefore1968_ntntC_se=r(se)
sca list incarcerationBefore1968_ntntC_se



/* Print the table */ 

qui mat Means=J(6,5,.)

qui mat rownames Means = "Arrested before 18" "SE" "Probation before 18" "SE" /// 
		 "Incarcerated before 1968" "SE" 
	
qui mat colnames Means = "Number of Observation" "Nt"  "At" "At & C" "Nt & C"

mat list Means


/* First column: Number of Observations */ 
sca val=-1
foreach name in firstArrestBy18 probationBy18 incarcerationBefore1968 {
  
  sca val=val+2
  
   display `"`name' mean"'
	
   mat Means[val,1]=num_`name'
   
		}

mat li Means

/* Second column: means and se of never-takers */ 
sca val=-1
foreach name in firstArrestBy18 probationBy18 incarcerationBefore1968  {
  
  sca val=val+2
  
   display `"`name' mean"'
	
   mat Means[val,2]=`name'_nt
   mat Means[val+1,2]=`name'_nt_se
		}

mat li Means

/* Third column: means and se of always-takers  */ 
sca val=-1
foreach name in  firstArrestBy18 probationBy18 incarcerationBefore1968  {
  
  sca val=val+2
  
   display `"`name' mean"'
	
   mat Means[val,3]=`name'_at
   mat Means[val+1,3]=`name'_at_se
		
		}

mat li Means


/* Fourth column: means and se of always-takers and compliers  */ 
sca val=-1

foreach name in firstArrestBy18 probationBy18 incarcerationBefore1968  {
  
  sca val=val+2
  
   display `"`name' mean"'
	
   mat Means[val,4]=`name'_atC
   mat Means[val+1,4]=`name'_atC_se
		
		}

mat li Means

/* Fifth column: mean and se of never-takers and compliers  */ 
sca val=-1
foreach name in firstArrestBy18 probationBy18 incarcerationBefore1968  {
  
  sca val=val+2
  
   display `"`name' mean"'
	
   mat Means[val,5]=`name'_ntC
   mat Means[val+1,5]=`name'_ntC_se
		
		}

mat li Means

/*  Differences in means for the strata */
qui mat Diffmeans=J(6,4,.)

qui mat rownames Diffmeans = "Arrested before 18" "SE" "Probation before 18" "SE" /// 
		 "Incarcerated before 1968" "SE" 
	
qui mat colnames Diffmeans = "Nt v.s. At"  "Nt v.s. At&C" "At v.s. At&C"  "Nt v.s. Nt&C"

mat list Diffmeans

/* First column: difference in means and se of never-takers v.s. always-takers */ 
sca val=-1

foreach name in firstArrestBy18 probationBy18 incarcerationBefore1968  {
  
  sca val=val+2
  
   display `"`name' difference in mean"'
	
   mat Diffmeans[val,1]=`name'_ntat
   mat Diffmeans[val+1,1]=se_`name'_ntat
		}

mat li Diffmeans

/* Second column: means and se of always-takers  */ 
sca val=-1
foreach name in firstArrestBy18 probationBy18 incarcerationBefore1968  {
  
  sca val=val+2
  
   display `"`name' mean"'
	
   mat Diffmeans[val,2]=`name'_ntatC
   mat Diffmeans[val+1,2]=`name'_ntatC_se
		
		}

mat li Diffmeans


/* Third column: means and se of always-takers and compliers  */ 
sca val=-1

foreach name in firstArrestBy18 probationBy18 incarcerationBefore1968  {
  
  sca val=val+2
  
   display `"`name' mean"'
	
   mat Diffmeans[val,3]=`name'_atatC
   mat Diffmeans[val+1,3]=`name'_atatC_se
		
		}

mat li Diffmeans

/* Fourth column: mean and se of never-takers and compliers  */ 
sca val=-1
foreach name in firstArrestBy18 probationBy18 incarcerationBefore1968  {
  sca val=val+2
  
   display `"`name' mean"'
	
   mat Diffmeans[val,4]=`name'_ntntC
   mat Diffmeans[val+1,4]=`name'_ntntC_se
		
		} 
		
mat li Means
mat li Diffmeans

